package SeparateWriter;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: Mike
 * Date: 12/11/11
 */

//    How to set up a new form & code set:
//    created a label for the form
//    right click to the get the binding wizard (2nd option)
//    create a main method


public class TemperatureReporter implements Runnable {
    private JTextArea filePath;
    private JButton filePathButton;
    private JButton startButton;
    private JButton stopButton;
    private JPanel reporterForm;
    private JTextArea writeDialog;
    static JFrame frame;
    public boolean writeOut = false;

    private double lastWrittenTemp;
    private double minTempChangeForWrite;


    //	@Override
    public void run() {
        frame = new JFrame("TemperatureReporter");
        frame.setContentPane(new TemperatureReporter().reporterForm);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }


    public TemperatureReporter() {
        filePathButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FileDialog d = new FileDialog(frame, "title", FileDialog.LOAD);
                d.setDirectory("C:");
                d.show();
                d.getFile();
                String file = d.getDirectory() + d.getFile();
                filePath.setText(file);
                System.out.println(d.getDirectory() + d.getFile());
            }
        });
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                writeOut = true;
                Data.writeOut = true;
                TempReadWrite TRW = new TempReadWrite();
//                TRW.setWriteOut(true);
                mainProgram.writeOut = true;
                writeDialog.setText(" Writing out temperature data.");
//                temperature();
//                Display.getDefault().asyncExec(new Runnable() {
//                    public void run() {
//                        mainProgram.writeOut = true;
//                    }
//                });
            }
        });
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                writeOut = false;
                TempReadWrite TRW = new TempReadWrite();
//                TRW.setWriteOut(true);

                mainProgram.writeOut = false;
                writeDialog.setText(" Stopped. Not writing out temperature data.");
            }
        });
    }

    public static void main(String[] args) {
        frame = new JFrame("TemperatureReporter");
        frame.setContentPane(new TemperatureReporter().reporterForm);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
